package com.sduept.nwld.dataserver.controller.daily;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;

import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.protection.NewDeviceManager;
import com.sduept.nwld.dataserver.model.daily.BreakerStartRecord;
import com.sduept.utils.DateUtil;

@Named
@ViewScoped
public class NewDeviceController extends DailyReportAbstractController {

	/**
	 * 新设备驱动及并网管理conteoller类
	 */
	private static final long serialVersionUID = 5595937197823216870L;

	@Autowired
	private NewDeviceManager nd;
	private List<BreakerStartRecord> devices = new ArrayList<>();
	private BreakerStartRecord selectDevice = null;
	private BreakerStartRecord created = null;

	private String bs0 = "0";
	private String bs1 = "0";
	private String bs2 = "0";
	private String bs3 = "0";
	private String bs4 = "0";
	private String bs5 = "0";
	private String bs6 = "0";
	private String bs7 = "0";
	private String bs8 = "0";

	@PostConstruct
	private void init() {
		devices = nd.getBreakerStartRecord(queryDate1, queryDate2);
	}

	public void queryByTime() {
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		queryDate2 = DateUtil.addDays(queryDate2, 1);
		queryDate2 = DateUtil.addSeconds(queryDate2, -1);
		devices = nd.getBreakerStartRecord(queryDate1, queryDate2);
		refreshQueryParamter();
		selectDevice = null;
	}

	public void resetQuery() {
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
		queryByTime();
		selectDevice = null;
		created = null;
	}

	public void update() {
		if (selectDevice != null) {
			nd.updateBreakerStartRecord(selectDevice);
			addSuccessMessage("保存成功");
		}
	}

	public void preEdit() {
		if (selectDevice != null) {
			created = selectDevice;
			bs0 = filter(created.getProjectDataMag());
			bs1 = filter(created.getSubstationCTMap());
			bs2 = filter(created.getProtectChannel());
			bs3 = filter(created.getValueInspect());
			bs4 = filter(created.getPlantTripRecord());
			bs5 = filter(created.getPrudentialSystemAccess());
			bs6 = filter(created.getNewEquiIn());
			bs7 = filter(created.getValueSetting());
			bs8 = filter(created.getOtherMajor());
		}
	}

	public String filter(String str) {
		if (StringUtils.isBlank(str)) {
			return "0";
		}
		if ("√".equals(str.trim())) {
			return "1";
		} else {
			return "2";
		}
	}

	public String getStyleClass(String str) {
		if (str != null && "√".equals(str.trim())) {
			return "correct";
		} else {
			return "error";
		}
	}

	public void deviceRowSelect(SelectEvent event) {
		selectDevice = (BreakerStartRecord) event.getObject();
	}

	public void createDevices() {
		created = new BreakerStartRecord();
		refreshBs();
	}

	private void refreshBs() {
		bs0 = "0";
		bs1 = "0";
		bs2 = "0";
		bs3 = "0";
		bs4 = "0";
		bs5 = "0";
		bs6 = "0";
		bs7 = "0";
		bs8 = "0";
	}

	public void saveNew() {
		if (created != null) {
			if (created.getId() == null) {
				created.setId(ObjectId.get().toString());
			}
			created.setProjectDataMag(getStr(bs0));
			created.setSubstationCTMap(getStr(bs1));
			created.setProtectChannel(getStr(bs2));
			created.setValueInspect(getStr(bs3));
			created.setPlantTripRecord(getStr(bs4));
			created.setPrudentialSystemAccess(getStr(bs5));
			created.setNewEquiIn(getStr(bs6));
			created.setValueSetting(getStr(bs7));
			created.setOtherMajor(getStr(bs8));
			nd.creatBreakerStartRecord(created);
		}
		queryByTime();
		created = null;
		addSuccessMessage("保存成功");
	}

	private String getStr(String b) {
		if (StringUtils.isBlank(b) || "0".equals(b)) {
			return null;
		}
		return "1".equals(b) ? "√" : "×";
	}

	public void deleteDevices() {
		if (selectDevice != null) {
			try {
				nd.delBreakerStartRecord(selectDevice);
				devices.remove(selectDevice);
				selectDevice = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			addErrorMessage("请选择一条数据进行删除");
		}
		addSuccessMessage("删除成功！");
	}

	public void DevicesCellEdit(CellEditEvent event) {
		selectDevice = null;
		for (BreakerStartRecord k : devices) {
			if (k.getId().equals(event.getRowKey())) {
				selectDevice = k;
				break;
			}
		}
		if (selectDevice != null)
			nd.updateBreakerStartRecord(selectDevice);
	}

	public List<BreakerStartRecord> getDevices() {
		return devices;
	}

	public void setDevices(List<BreakerStartRecord> devices) {
		this.devices = devices;
	}

	public BreakerStartRecord getCreated() {
		return created;
	}

	public void setCreated(BreakerStartRecord created) {
		this.created = created;
	}

	public BreakerStartRecord getSelectDevice() {
		return selectDevice;
	}

	public void setSelectDevice(BreakerStartRecord selectDevice) {
		this.selectDevice = selectDevice;
	}

	public NewDeviceManager getNd() {
		return nd;
	}

	public void setNd(NewDeviceManager nd) {
		this.nd = nd;
	}

	public String getBs0() {
		return bs0;
	}

	public void setBs0(String bs0) {
		this.bs0 = bs0;
	}

	public String getBs1() {
		return bs1;
	}

	public void setBs1(String bs1) {
		this.bs1 = bs1;
	}

	public String getBs2() {
		return bs2;
	}

	public void setBs2(String bs2) {
		this.bs2 = bs2;
	}

	public String getBs3() {
		return bs3;
	}

	public void setBs3(String bs3) {
		this.bs3 = bs3;
	}

	public String getBs4() {
		return bs4;
	}

	public void setBs4(String bs4) {
		this.bs4 = bs4;
	}

	public String getBs5() {
		return bs5;
	}

	public void setBs5(String bs5) {
		this.bs5 = bs5;
	}

	public String getBs6() {
		return bs6;
	}

	public void setBs6(String bs6) {
		this.bs6 = bs6;
	}

	public String getBs7() {
		return bs7;
	}

	public void setBs7(String bs7) {
		this.bs7 = bs7;
	}

	public String getBs8() {
		return bs8;
	}

	public void setBs8(String bs8) {
		this.bs8 = bs8;
	}

}
